<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="登录地址" prop="ipaddr">
        <el-input v-model="queryParams.ipaddr" placeholder="请输入登录地址" clearable size="small" @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="用户名称" prop="userName">
        <el-input v-model="queryParams.userName" placeholder="请输入用户名称" clearable size="small" @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <search-button @click="handleQuery" :perm="['monitor:online:search']" />
        <reset-button @click="resetQuery" :perm="['monitor:online:search']" />
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="list.slice((pageNum-1)*pageSize,pageNum*pageSize)" style="width: 100%;">
      <el-table-column label="序号" type="index" align="center" width="60">
        <template #default="scope">
          <span>{{ (pageNum - 1) * pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="会话编号" align="center" prop="tokenId" min-width="120" :show-overflow-tooltip="true" />
      <el-table-column label="登录名称" align="center" prop="userName" :show-overflow-tooltip="true" />
      <el-table-column label="主机" align="center" prop="ipaddr" :show-overflow-tooltip="true" />
      <el-table-column label="登录地点" align="center" prop="loginLocation" :show-overflow-tooltip="true" />
      <el-table-column label="浏览器" align="center" prop="browser" />
      <el-table-column label="操作系统" align="center" prop="os" />
      <el-table-column label="登录时间" align="center" prop="loginTime" width="160">
        <template #default="scope">
          {{ formatLoginTime(scope.row.loginTime) }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <text-delete-button
            @click="handleForceLogout(scope.row)" :perm="['monitor:online:forceLogout']" text="强退"
            :title="'是否确认强制退出用户'+scope.row.userName+'?'" />
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" v-model:page="pageNum" v-model:limit="pageSize" />
  </div>
</template>

<script>
import { list, forceLogout } from "@monitor/api/monitor/online";
import moment from "moment";

export default {
  name: "MonitorOnline",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      pageNum: 1,
      pageSize: 10,
      // 查询参数
      queryParams: {
        ipaddr: undefined,
        userName: undefined
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询登录日志列表 */
    getList() {
      this.loading = true;
      list(this.queryParams).then(response => {
        this.list = response.data;
        // this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 强退按钮操作 */
    handleForceLogout(row) {
      forceLogout(row.tokenId)
        .then(() => {
          this.getList();
          this.msgSuccess("强退成功");
        }).catch(function () { });
    },
    formatLoginTime(loginTime) {
      return parseInt(loginTime) > 0 ? moment(new Date(parseInt(loginTime))).format("YYYY-MM-DD HH:mm:SS") : '-';
    }
  }
};
</script>

